﻿/////////////////////////////////////////////////////////////////////////////////////////////
// <copyright file="OnPremiseMailUser.cs" company="Microsoft Corporation">
//     Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
/////////////////////////////////////////////////////////////////////////////////////////////

namespace Microsoft.LiveAtEdu.Samples.Powershell.OnPremise
{
    using System;
    using System.Collections.ObjectModel;
    using System.Management.Automation;

    /// <summary>
    /// Powershell commands for a creating mailuser.
    /// </summary>
    public class OnPremiseMailUser
    {
        /// <summary>
        /// Session runsapce instance.
        /// </summary>
        private readonly SessionRunspace sessionRunspace;

        /// <summary>
        /// Initializes a new instance of the OnPremiseMailUser class.
        /// </summary>
        /// <param name="sessionRunspace">SessionRunspace instance.</param>
        public OnPremiseMailUser(SessionRunspace sessionRunspace)
        {
            if (sessionRunspace == null)
            {
                throw new ArgumentOutOfRangeException("sessionRunspace");
            }

            this.sessionRunspace = sessionRunspace;
        }

        /// <summary>
        /// Creates a mailUser in on-premise exchange. This automatically creates an AD account. 
        /// </summary>
        /// <param name="name">Display name.</param>
        /// <param name="firstName">First Name</param>
        /// <param name="lastName">Last Name.</param>
        /// <param name="externalEmailAddress">Outlook live ID</param>
        /// <param name="upnName">User Principal Name</param>
        /// <param name="password">User Password</param>
        /// <param name="organizationUnit">Organization Unit</param>
        /// <param name="userRole">User Role.</param>
        /// <param name="department">Name of the department.</param>
        /// <param name="company">Name of the company.</param>
        /// <returns>Return true if a mailbox is created.</returns>
        public bool CreateOnPremiseExchangeMailUser(
            string name,
            string firstName,
            string lastName,
            string externalEmailAddress,
            string upnName,
            string password,
            string organizationUnit,
            string userRole,
            string department,
            string company)
        {
            var args = new string[]
            {
                name, firstName, lastName, externalEmailAddress, upnName, password, organizationUnit, userRole, department, company
            };

            var command = new PSCommand();
            command.AddScript(string.Format(MailUserScripts.CreateOnPremiseExchangeMailUserScript, args));

            var accounts = this.sessionRunspace.ExecuteCommand(command);

            if (accounts != null && accounts.Count == 1)
            {
                return true;
            }

            return false;
        }
    }
}
